package dj.servlet;

import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.web.context.support.WebApplicationContextUtils;

import dj.Constant;
import dj.service.FriendService;
import dj.service.RepairService;
import dj.service.UserService;
import dj.timer.TaskTimer;
import dj.timer.UsgeReportSms;
import dj.utils.TableNote;
import dj.webservice.mas.org.csapi.schema.common.v2_0.CMAbility;
import dj.webservice.mas.org.csapi.schema.common.v2_0.MessageNotificationType;
import dj.webservice.mas.org.csapi.schema.notification.StartNotificationRequest;
import dj.webservice.mas.org.csapi.service.CmccMasWbsClient;

public class StartServlet extends HttpServlet {

	private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("config");

	@Override
	public void init() throws ServletException {
		super.init();

		Constant.TOMCAT_PATH = getServletContext().getRealPath("/");

		Constant.CONTEXT_PATH = getServletContext().getContextPath();

		Constant.MAS_WEBSERVICE_ADDR = bundle.getString("masWebserviceAddr");

		Constant.NOTIFY_MAS_ADDR = bundle.getString("notifyMasAddr");

		Constant.GET_USER_FROM_DB = Boolean.parseBoolean(bundle.getString("getUserFromDb")); // 是否从数据库加载用户及组信息等

		// 初始化mas短信接口
		Constant.smws = new CmccMasWbsClient().getCmccMasWbsSoap();
		StartNotificationRequest snr = new StartNotificationRequest();
		snr.setApplicationId(Constant.APPLICATIONID);
		MessageNotificationType mnt = new MessageNotificationType();
		mnt.setCMAbility(CMAbility.SMS_ABILITY);
		mnt.getWSURI().add(Constant.NOTIFY_MAS_ADDR);
		snr.getMessageNotification().add(mnt);
		// 通知
		Constant.smws.startNotification(snr);

		Constant.ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); // 把spring上下文放到常用参数中，启动时初始化，其他地方也可以用
		
		new TaskTimer().usgeReportTimer(new UsgeReportSms(), bundle.getString("sendSmsTime"));

		RepairService repairService = (RepairService) Constant.ac.getBean("repairService");

		// 是否初始化菜单和角色
		if (Boolean.parseBoolean(bundle.getString("repairMenu"))) {
			repairService.repairMenu();
		}
		// 如果基本组不存在，则创建
		// repairService.repairFriendGroup();

		if (!Constant.GET_USER_FROM_DB) {
			UserService userService = (UserService) Constant.ac.getBean("userService");
			userService.initUserMap(); // 初始化用户

			FriendService friendService = (FriendService) Constant.ac.getBean("friendService");
			friendService.initFriendGroupMap();
			friendService.initFriendMap();
		} else {
			// 初始化公共好友组
			FriendService friendService = (FriendService) Constant.ac.getBean("friendService");
			friendService.initCommonFriendGroup();
		}
		// 初始化聊天记录
		// TalkContextService tcs = (TalkContextService) Constant.ac.getBean("talkContextService");
		// tcs.initUnreadContext();

		// 启动消息自动插入数据库线程
		// new Thread(new TalkContextThread()).start();

		// 初始化管理员
		repairService.repairAdmin();
		// 初始化操作注释
		TableNote.initTranslationMap();
	}

}
